package cn.eoeit.alone.phoneImport.fragment;



import java.util.ArrayList;
import java.util.List;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;


import cn.eoeit.alone.phoneImport.MainActivity;
import cn.eoeit.alone.phoneImport.R;
import cn.eoeit.alone.phoneImport.SysApplication;
import cn.eoeit.alone.phoneImport.activity.LoginActivity;
import cn.eoeit.alone.phoneImport.adapter.PhoneRecordAdapter;
import cn.eoeit.alone.phoneImport.db.CacheDbAgent;
import cn.eoeit.alone.phoneImport.model.PhoneRecord;
import cn.eoeit.alone.phoneImport.util.Constants;
import cn.eoeit.alone.phoneImport.util.ScreenUtils;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class PhoneFragment extends Fragment implements OnClickListener {

	private View layout;
	private View layoutNull;
	private PhoneRecordAdapter mAdapter;
	private View mViewLogin;
	private SwipeMenuListView mListView;
	private TextView mTvAddUp;
	private ProgressBar mProgressBar;
	
	private ArrayList<PhoneRecord> mData = new ArrayList<PhoneRecord>();
	

	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout != null) {
			// ��ֹ���new��Ƭ�ζ������ͼƬ��������
			return layout;
		}
		layout = inflater.inflate(R.layout.fragment_cart, container, false);
		initView();
		setOnListener();
		initListView();
		initData();
		return layout;
	}

	private void setOnListener() {
		layout.findViewById(R.id.btn_createNew).setOnClickListener(this);
		layout.findViewById(R.id.btn_more).setOnClickListener(this);
	}

	private void initView() {
		layoutNull = layout.findViewById(R.id.layout_null);
		mViewLogin = layout.findViewById(R.id.layout_login_cart);
		mProgressBar = (ProgressBar) layout.findViewById(R.id.progressBar_cart);
	}

	private void initData() {
		// �첽����ݿ��л�ȡ���
		new InCartTask().execute();
	}

	


	private void initListView() {
		mListView = (SwipeMenuListView) layout.findViewById(R.id.listView_cart);
		View foot = getActivity().getLayoutInflater().inflate(
				R.layout.foot_cart_list, null);
		
		mTvAddUp = (TextView) foot.findViewById(R.id.tv_add_up);
		mListView.addFooterView(foot, null, false);
		mAdapter = new PhoneRecordAdapter();
		mListView.setAdapter(mAdapter);
		// step 1. create a MenuCreator
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// create "open" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
						0x3F, 0x25)));
				// set item width
				deleteItem
						.setWidth(ScreenUtils.getScreenWidth(getActivity()) / 3);
				// set item title
				deleteItem.setTitle("ɾ��");
				// set item title fontsize
				deleteItem.setTitleSize(18);
				// set item title font color
				deleteItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(deleteItem);

			}
		};
		// set creator
		mListView.setMenuCreator(creator);

		// step 2. listener item click event
		mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {		
				deleteItem(position);
			}
		});
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			}
		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		ViewGroup parent = (ViewGroup) layout.getParent();
		parent.removeView(layout);
	}
	
	/**
	 * ɾ���б���
	 */
	private void deleteItem(int position) {
		
		//DBUtils.deleteCart();
	
		mData.remove(position);
		mAdapter.notifyDataSetChanged();

	}



	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
	}

	
	class InCartTask extends AsyncTask<Void, Void, List<PhoneRecord>> {

		@Override
		protected List<PhoneRecord> doInBackground(Void... params) {
			CacheDbAgent dbHelper= ((SysApplication)getActivity().getApplicationContext()).getCacheDbAgent();
			return  dbHelper.getPhoneRecord();
		}

		@Override
		protected void onPostExecute(List<PhoneRecord> result) {
			super.onPostExecute(result);
			mData.clear();
			mData.addAll(result);
			mAdapter.notifyDataSetChanged();
			if (mData.size() == 0) {
				mListView.setVisibility(View.GONE);
			} else {
				mListView.setVisibility(View.VISIBLE);
			}
			mProgressBar.setVisibility(View.GONE);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_createNew: // ��¼
		case R.id.btn_more: // ȥ��ɱ
			MainActivity activity = (MainActivity) getActivity();
			activity.activeCreatePhoneRecord();
			break;

		default:
			break;
		}
	}

}

